/**
 * Created by cchen on 2015/6/8.
 */
'use strict';

angular.module('backofficeApp.bannerList.controllers', []).
    controller('bannerListCtrl', ['$scope', '$routeParams', 'CommunityService', '$http', '$location', function ($scope, $routeParams, communityService, $http, $location) {

        $scope.currentPage = 1;
        $scope.numPages = 999;
        $scope.imgHost = imgHost;

        $scope.bannerType = [
            {
                name: 'banner',
                value: 1
            },
            {
                name: '话题公告',
                value: 2
            }
        ];

        $scope.value = Number($routeParams.type);

        $scope.getData = function () {
            if ($scope.value == null) {
                $scope.value = 3;
            }
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/getBannerList/' + $scope.value + '/' + $scope.currentPage, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.totalPage = data.data.totalPage;
                $scope.bannerList = data.data.bannerList;
            }).error(function () {
            });
        };

        $scope.getData();

        $scope.clickAddBanner = function () {
            $location.path('/bannerAdd/' + $scope.value)
        };

        $scope.bannerEdit = function (bannerId) {
            $location.path('/bannerEdit/' + bannerId)
        };

        $scope.bannerDelete = function (bannerId) {
            bootbox.confirm('是否确定删除?', function (result) {
                if (result) {
                    $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/deleteCommunityBanner/' + bannerId, {
                        headers: {'Content-Type': 'application/json;charset=utf-8'}
                    }).success(function (data) {
                        $scope.getData();
                    }).error(function () {
                    });
                }
            }).find("div.modal-dialog").css({width: '30%'});
        };

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getData();
        };

        $scope.allCheck = function (ck,openType) {

            if (!ck) {
                $scope.banners = [];

                $("[name = chkItem]:checkbox").prop("checked", true);
                $scope.bannerList.forEach(function (banner) {
                    $scope.banners.push(banner)
                })
            } else {
                $scope.banners = [];

                $("[name = chkItem]:checkbox").prop("checked", false);
                $scope.bannerList.forEach(function (banner) {
                    $scope.banners.remove(banner);
                })
            };
            //console.debug($scope.questions);
        };

        $scope.checkChange = function(ckc){
            if($scope.ck){
                $scope.ck = false;
            };
        };

        $scope.banners = new Array();
        $scope.bannerToggle = function (banner) {

            var isexist = false;
            $scope.banners.forEach(function (item) {
                if(item.objId == banner.objId){
                    isexist = true;
                };
            });
            if(isexist){
                $scope.banners.remove(banner);
            }else{
                $scope.banners.push(banner);
            };
            console.info($scope.banners)
        };

        $scope.deleteChkBanner = function (bannerId) {
            bootbox.confirm('是否确定删除?', function (result) {
                if (result) {
                    var opt = {
                        banners :  $scope.banners
                    };

                    $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/deleteBannerByFilters',opt, {
                        headers: {'Content-Type': 'application/json;charset=utf-8'}
                    }).success(function (data) {
                        $scope.getData();
                    }).error(function () {
                    });
                }
            }).find("div.modal-dialog").css({width: '30%'});
        };

    }]).directive('paging', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {

                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.numPages ? scope.numPages : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('numPages', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.numPages;
                };

            }
        };
    });